from django.shortcuts import render

# Create your views here.
import cv2
from django.http import StreamingHttpResponse
from django.views.decorators import gzip

@gzip.gzip_page
def stream_video(request):
    cap = cv2.VideoCapture(0)
    fps = cap.get(cv2.CAP_PROP_FPS)
    print(fps)
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    print(size)
    def generate_frames():
        while True:
            ret, frame = cap.read()
            if not ret:
                print('not camer')
                break

            _, jpeg = cv2.imencode('.jpg', frame)
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + jpeg.tobytes() + b'\r\n\r\n')
    #return render(request, 'stream.html')

    return StreamingHttpResponse(generate_frames(), content_type='multipart/x-mixed-replace; boundary=frame')
    #return render(request, 'stream.html')

def stream_video_view(request):
    return render(request, 'stream.html')
